package net.hn.hnms.biz.mine.service;

import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.feign.model.DataStateVo;
import net.hn.hnms.biz.api.mine.RemoteEquipmentService;
import net.hn.hnms.biz.api.mine.RemotePowerService;
import net.hn.hnms.biz.api.safety.dust.fegin.RemoteDustService;
import net.hn.hnms.biz.api.safety.fire.fegin.RemoteFrieService;
import net.hn.hnms.biz.api.safety.location.fegin.RemoteLocationService;
import net.hn.hnms.biz.api.safety.monitor.fegin.RemoteMonitorService;
import net.hn.hnms.biz.api.safety.support.fegin.RemoteSupportService;
import net.hn.hnms.biz.api.safety.trinity.fegin.RemoteTrinityService;
import net.hn.hnms.biz.api.safety.water.fegin.RemoteWaterService;
import net.hn.hnms.biz.mine.domain.DataStateConfig;
import net.hn.hnms.biz.mine.domain.vo.OptionVo;
import net.hn.hnms.biz.mine.repository.DataStateConfigRepository;
import net.hn.hnms.sys.common.core.constant.MineBaseConstants;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Author: PanSX
 * Date: 2023/5/31 14:51
 */
@Service
@RequiredArgsConstructor
public class DataStateService {
    private final DataStateConfigRepository dataStateConfigRepository;
    private final RemoteMonitorService remoteMonitorService;
    private final RemoteLocationService remoteLocationService;
    private final RemoteEquipmentService remoteEquipmentService;
    private final RemotePowerService remotePowerService;
    private final RemoteTrinityService remoteTrinityService;
    private final RemoteWaterService remoteWaterService;
    private final RemoteFrieService remoteFrieService;
    private final RemoteSupportService remoteSupportService;
    private final RemoteDustService remoteDustService;
    private Date current;

    public List<DataStateConfig> getStates(String sysName) {
        List<DataStateConfig> all = dataStateConfigRepository.findAll();
        if (StringUtils.isNotBlank(sysName)) {
            all = all.stream().filter(u -> u.getSysName().contains(sysName)).collect(Collectors.toList());
        }
        //排序
        all.sort(Comparator.comparing(DataStateConfig::getId));
        List<DataStateConfig> list = new ArrayList<>();
        current = new Date();
        for (DataStateConfig config : all) {
            List<DataStateVo> vos = new ArrayList<>();
            switch (config.getSysName()) {
                case "安全监控":
                    R<List<DataStateVo>> r = remoteMonitorService.getMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r.getCode() == 200) {
                        vos = r.getData();
                    }
                    break;
                case "人员定位":
                    R<List<DataStateVo>> r1 = remoteLocationService.getMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r1.getCode() == 200) {
                        vos = r1.getData();
                    }
                    break;
                case "压风":
                case "通风":
                case "主排水":
                case "煤流":
                case "提升":
                case "顶板":
                case "综采":
//                case "协同开采":
                case "瓦斯抽放":
                case "综掘":
                case "制氮":
                    R<List<DataStateVo>> r2 = remoteEquipmentService.getMaxTime(config.getMineCode(), config.getCompanyCode(), config.getSysName());
                    if (r2.getCode() == 200) {
                        vos = r2.getData();
                    }
                    break;
                case "供电":
                    R<List<DataStateVo>> r3 = remotePowerService.getMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r3.getCode() == 200) {
                        vos = r3.getData();
                    }
                    break;
                case "双预控-风险":
                    R<List<DataStateVo>> r4 = remoteTrinityService.getRiskMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r4.getCode() == 200) {
                        vos = r4.getData();
                    }
                    break;
                case "双预控-隐患":
                    R<List<DataStateVo>> r5 = remoteTrinityService.getHiddenDangerMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r5.getCode() == 200) {
                        vos = r5.getData();
                    }
                    break;
                case "水害-长观孔":
                    R<List<DataStateVo>> r6 = remoteWaterService.getHolerealMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r6.getCode() == 200) {
                        vos = r6.getData();
                    }
                    break;
                case "水害-排水":
                    R<List<DataStateVo>> r7 = remoteWaterService.getPSLMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r7.getCode() == 200) {
                        vos = r7.getData();
                    }
                    break;
                case "水害-涌水":
                    R<List<DataStateVo>> r8 = remoteWaterService.getYSLMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r8.getCode() == 200) {
                        vos = r8.getData();
                    }
                    break;
                case "束管":
                    R<List<DataStateVo>> r9 = remoteFrieService.getMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r9.getCode() == 200) {
                        vos = r9.getData();
                    }
                    break;
                case "支架阻力":
                    R<List<DataStateVo>> r10 = remoteSupportService.getZjzlMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r10.getCode() == 200) {
                        vos = r10.getData();
                    }
                    break;
                case "顶板离层":
                    R<List<DataStateVo>> r11 = remoteSupportService.getDblcMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r11.getCode() == 200) {
                        vos = r11.getData();
                    }
                    break;
                case "锚杆索":
                    R<List<DataStateVo>> r12 = remoteSupportService.getMgylMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r12.getCode() == 200) {
                        vos = r12.getData();
                    }
                    break;
                case "粉尘":
                    R<List<DataStateVo>> r13 = remoteDustService.getMaxTime(config.getMineCode(), config.getCompanyCode());
                    if (r13.getCode() == 200) {
                        vos = r13.getData();
                    }
                    break;
            }
            addDataState(list, vos, config);
        }

        return list;
    }

    private void addDataState(List<DataStateConfig> list, List<DataStateVo> vos, DataStateConfig currentConfig) {
        if (vos.isEmpty()) {

            if (StringUtils.isNotBlank(currentConfig.getMineCode())) {
                DataStateConfig config = new DataStateConfig();
                config.setMineCode(currentConfig.getMineCode());
                config.setSysName(currentConfig.getSysName());
                config.setIntervalSecond(currentConfig.getIntervalSecond());
                MineBaseConstants.Mine mine = MineBaseConstants.mineMap.get(currentConfig.getMineCode());
                if (mine != null) {
                    config.setMineName(mine.getMineName());
                    config.setCompanyName(mine.getCompanyName());
                }
                config.setState("无数据");
                list.add(config);
            } else {
                for (MineBaseConstants.Mine mine : MineBaseConstants.getMineMapByCompanyCode(currentConfig.getCompanyCode())) {
                    DataStateConfig config = new DataStateConfig();
                    config.setMineCode(currentConfig.getMineCode());
                    config.setIntervalSecond(currentConfig.getIntervalSecond());
                    config.setSysName(currentConfig.getSysName());
                    config.setMineName(mine.getMineName());
                    config.setCompanyName(mine.getCompanyName());
                    config.setState("无数据");
                    list.add(config);
                }
            }
        }

        for (DataStateVo vo : vos) {
            DataStateConfig config = new DataStateConfig();
            config.setMineCode(currentConfig.getMineCode());
            config.setSysName(currentConfig.getSysName());
            Date maxTime = vo.getMaxTime();
            config.setIntervalSecond(currentConfig.getIntervalSecond());
            config.setMaxTime(maxTime);


            if (maxTime == null) {
                config.setState("断线");
            } else if (current.getTime() - maxTime.getTime() < currentConfig.getIntervalSecond() * 1000) {
                config.setState("在线");
            } else {
                config.setState("离线");
            }
            if (maxTime != null) {
                long l = (current.getTime() - maxTime.getTime()) / 1000;
//                config.setInterval(l/60+"分钟"+l%60+"秒");
                long day = TimeUnit.SECONDS.toDays(l);
                long hours = TimeUnit.SECONDS.toHours(l) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(l));
                long minutes = TimeUnit.SECONDS.toMinutes(l) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(l));
                long seconds = TimeUnit.SECONDS.toSeconds(l) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(l));
                String duration = "%s%s%s%s";
                config.setInterval(String.format(duration, day > 0 ? day + "天" : "", hours > 0 ? hours + "小时" : "", minutes > 0 ? minutes + "分钟" : "", seconds > 0 ? seconds + "秒" : ""));
            }
            MineBaseConstants.Mine mine = MineBaseConstants.mineMap.get(vo.getMineCode());
            if (mine != null) {
                config.setMineName(mine.getMineName());
                config.setCompanyName(mine.getCompanyName());
            }
            list.add(config);
        }
    }

    public List<OptionVo> getOnlineRate() {
        List<OptionVo> result = new ArrayList<>();
        List<DataStateConfig> states = getStates(null);
        int onlineCount = (int) states.stream().filter(u -> u.getState().equals("在线")).count();
        result.add(new OptionVo("在线", onlineCount + ""));
        result.add(new OptionVo("离线", states.size() - onlineCount + ""));
        return result;
    }
}
